﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmCustomerUpdate : Form
    {
        DatabaseConnection dbcon = new DatabaseConnection();
        public frmCustomerUpdate()
        {
            InitializeComponent();
            btnbrowse.Click += new EventHandler(btnbrowse_Click);
            btnclose.Click += new EventHandler(btnclose_Click);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            //Error Trap for null values
            #region
            if (txtCustomerID.Text == "")
            {
                MessageBox.Show("Please Browse Customer to Update","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                txtCustomerID.Focus();
                return;
            }
            if (txtCustomerName.Text == "")
            {
                MessageBox.Show("Please Enter Customer name to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCustomerName.Text = "";
                txtCustomerName.Focus();
                return;
            }
            if (txtAddress.Text == "")
            {
                MessageBox.Show("Please Enter Customer address to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAddress.Text = "";
                txtAddress.Focus();
                return;
            }
            if (txtCity.Text == "")
            {
                MessageBox.Show("Please Enter Customer City to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCity.Text = "";
                txtCity.Focus();
                return;
            }
            if (textBox1.Text== "")
            {
                MessageBox.Show("Please Enter Customer Contact Number to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Text = "";
                textBox1.Focus();
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("Please Browse Customer Alternate Contact Number to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox2.Text = "";
                textBox2.Focus();
                
                return;
            }
            if (txtEmail.Text == "")
            {
                MessageBox.Show("Please Browse Customer to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Text = "";
                txtEmail.Focus();
            }
#endregion
            dbcon.dbconnection();
            dbcon.UpdateData("Update tblcustomerprofile set Customername ='" + txtCustomerName.Text + "', Address = '" + txtAddress.Text + "' ,City = '" +txtCity.Text + "', Contactno = '" + this.textBox1.Text + "', AltContactno = '" + this.textBox2.Text + "', Email = '"+txtEmail.Text + "' Where CustomerID='" + txtCustomerID.Text + "';");
            MessageBox.Show("Update Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            reset();
            dbcon.DBClose(); 
            
        }
        void reset()
        {
            txtCustomerID.Text = "";
            txtCustomerName.Text = "";
            txtAddress.Text = "";
            textBox1.Text = "";
            textBox2.Text = "";
            txtCity.Text = "";
            txtEmail.Text = "";
        
        
        }

        void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        void btnbrowse_Click(object sender, EventArgs e)
        {
            frmCustomerList frmc = new frmCustomerList();
            frmc.Show();
            this.Hide();
        }
        
    }
}
